--
-- Author: shunguo.chen
-- Date: 2019-05-17 16:51:03
--
-- LegendStarMap   传奇星图
--

local LegendStar = import(".LegendStar")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
    if data then
        self:init(data)
    end
end

-- message StarMap {
--     int32 ID                = 1;
--     bool State           = 2;    //星图是否被激活
--     int32 Progress  =   3;
--     repeated StarMapSkill StarMapSkills = 4;    //该数据无序
-- }
function ClassRef:init(data)
    local preId = self.id
    self.id = data.ID
    self._isActive = data.State
    self.pg = data.Progress/100

    local starDict = {}
    for i,v in ipairs(data.StarMapSkills) do
        local star = LegendStar.new(v)
        starDict[star.id] = star
    end
    self.starDict = starDict

    if self.id ~= preId then
        self._conf = GD:getLegendStarMapConf(self.id)

        self.lv = tonumber(self._conf.floor or 1)
        self.limitLv = tonumber(self._conf.limitLV or 0)
        self.limitPg = tonumber(self._conf.limitProgress or 0) / 100
    end
end

function ClassRef:getId()
    return self.id
end

function ClassRef:isFull()
    return self.pg >= 100
end

-- 获取星魂前置满足条件的位置
function ClassRef:getStarOpenPreSeatParams(star)
    local conditions = star.conditions
    local preSeatParams = {}
    local preStar, seatParam
    for i,condition in ipairs(conditions) do
        preStar = self.starDict[condition.starId]
        if preStar then
            local seatParam = {}
            seatParam.seat = preStar.seat
            seatParam.isOpen = preStar.lv >= condition.starLv
            preSeatParams[#preSeatParams + 1] = seatParam
        end
    end
    return preSeatParams
end

function ClassRef:isStarCanActive(star)
    local conditions = star.conditions
    if #conditions > 0 then
        -- 有一个条件满足即可激活
        local preStar
        for i,condition in ipairs(conditions) do
            preStar = self.starDict[condition.starId]
            if preStar and preStar.lv >= condition.starLv then
                return true
            end
        end
    else
        return true
    end
end

-- 获取星图所有属性集合
function ClassRef:getStarPropDict()
    local propDict = {}
    for i,star in pairs(self.starDict) do
        local propId, propValue = star:getCurStarProp()
        if propId then
            if propDict[propId] then
                propDict[propId] = propDict[propId] + propValue
            else
                propDict[propId] = propValue
            end
        end
    end
    return propDict
end

-- 激活
function ClassRef:setActive(b)
    self._isActive = b
end
function ClassRef:isActive()
    return self._isActive
end

return ClassRef